<template>
  <div class="movie_body">
    <loading v-if="flag"></loading>
    <Scroll v-else :handleToScroll="handleToScroll" :handleToTouchEnd="handleToTouchEnd">
      <ul>
        <li class="pullDown">{{ pullDownMsg }}</li>
        <li v-for="item in movieList" :key="item.id">
          <div class="pic_show" @tap="handlToDetail(item.id)">
            <!-- 过滤器 -->
            <!-- <img :src="item.img | setWH('128.180')" /> -->
            <img :src="item.img" />
          </div>
          <div class="info_list">
            <h2 @tap="handlToDetail(item.id)">{{item.nm}}</h2>
            <img v-if="item.version" src="@/assets/maxs.png" alt />
            <p>
              观众评
              <span class="grade">{{item.sc}}</span>
            </p>
            <p>主演:{{item.star}}</p>
            <p>{{item.showInfo}}</p>
          </div>
          <div class="btn_mall">购票</div>
        </li>
      </ul>
    </Scroll>
  </div>
</template>

<script>
import { getmovielist } from "network/nowplaying";

//不是全局注册的话就要这样
import Scroll from "components/Srcoll/srcoll";
import loading from "components/Loading";


//引入better-srcoll插件  应该封装起来的
// import BScroll from "better-scroll";
export default {
  name: "NowPlaying",
  components: {
    Scroll,
    loading
  },
  data() {
    return {
      movieList: [],
      // 上拉加载更多的样式
      pullDownMsg: "",
      // 默认是true 加载的时候显示
      flag:true,
      // 控制重复发送请求
      prevCityid:-1
    };
  },
  // 组件活跃到时候执行
  activated() {
    var cityid = this.$store.state.city.id;
    if(this.prevCityid === cityid) {return;} //如果相等就说明不执行 直接组件本生命周期的函数
    //点击城市的时候就发送id过去替换新的数据  切换的时候 再次发送请求
    this.flag =true
    this.getmovielist(cityid);
  },
  mounted() {
    var cityid = this.$store.state.city.id;
    this.getmovielist(cityid);
  },
  methods: {
    //获取正在热映信息
    getmovielist(cityid) {
      getmovielist(cityid).then((res) => {
        // var msg = res.msg;
        // if(msg === 'ok') {
            // this.flag = false  //加载完成后false关闭
          //  this.prevCityid = cityid
        //   this.movieList = res.data.movieList;
        // }

      });
    },
    //tap事件 要配置bterll-srcoll事件才行 跳转详情页的
    handlToDetail(id) {
      this.$router.push('/movie/detail/1/' + id)
    },

    // 实现上拉加载更多并且替换样式 scroll touchEnd 二个方法实现
    handleToScroll(pos) {
      //pos滚动的距离
      if (pos.y > 30) {
        this.pullDownMsg = "正在更新中";
      }
    },
    handleToTouchEnd(pos) {
      if (pos > 30) {
        //滑动结束 重新发送请求 用户体验加个定时器 来实现 数据的缓慢渲染
        getmovielist().then((res) => {
           var msg = res.msg;
           if (msg === "ok") {
              this.flag = false  //加载完成后false关闭
             this.pullDownMsg = "更新成功";
             setTimeout(() => {
              this.movieList = res.data.data.movieList;
               this.pullDownMsg = "";
             }, 1000);
           }
        });
      }
    },
  },
  
};
</script>

<style scoped>
#content .movie_body {
  flex: 1;
  overflow: auto;
}
.movie_body ul {
  margin: 0 12px;
  overflow: hidden;
}
.movie_body ul li {
  margin-top: 12px;
  display: flex;
  align-items: center;
  border-bottom: 1px #e6e6e6 solid;
  padding-bottom: 10px;
}
.movie_body .pic_show {
  width: 64px;
  height: 90px;
}
.movie_body .pic_show img {
  width: 100%;
}
.movie_body .info_list {
  margin-left: 10px;
  flex: 1;
  position: relative;
}
.movie_body .info_list h2 {
  font-size: 17px;
  line-height: 24px;
  width: 150px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.movie_body .info_list p {
  font-size: 13px;
  color: #666;
  line-height: 22px;
  width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.movie_body .info_list .grade {
  font-weight: 700;
  color: #faaf00;
  font-size: 15px;
}
.movie_body .info_list img {
  width: 50px;
  position: absolute;
  right: 10px;
  top: 5px;
}
.movie_body .btn_mall,
.movie_body .btn_pre {
  width: 47px;
  height: 27px;
  line-height: 28px;
  text-align: center;
  background-color: #f03d37;
  color: #fff;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
.movie_body .btn_pre {
  background-color: #3c9fe6;
}
.movie_body .pullDown {
  margin: 0;
  padding: 0;
  border: none;
}
</style>
